#include "waitwidget.h"
#include <QCoreApplication>
#include <QtMath>
#include <QPainter>
#include <QTimer>
#include <QDebug>

#define PI 3.14159265358979323846

waitWidget::waitWidget(QWidget *parent,int inCircle_r,int outCircle_r,int LineWidth) :
    QWidget(parent)
{
    //ui->setupUi(this);

    if (this->objectName().isEmpty())
        this->setObjectName(QString::fromUtf8("waitWidget"));
    this->resize(400, 300);
    this->setWindowTitle(QCoreApplication::translate("waitWidget", "Form", nullptr));
    QMetaObject::connectSlotsByName(this);

    QSize size = parent->size();
    this->setGeometry(0,80,size.width(),size.height() - 80);
    this->setWindowOpacity(0);
    inCirclePoint  = new QPoint(size.width() / 2 , (size.height()-80) / 2);
    outCirclePoint  = new QPoint(size.width() / 2 ,  (size.height()-80)  / 2);
    myInCircle_r = inCircle_r;
    myOutCircle_r = outCircle_r;
    myLineWidth = LineWidth;
    LoopTimer = new QTimer(this);
    LoopTimer->start(40);
    for(int a = 0;a< EQUALNUM;a++){
        myPainter[a] = new QPainter(this);
    }
    connect(LoopTimer,SIGNAL(timeout()),this,SLOT(loopTimerOut()));
}

waitWidget::~waitWidget()
{
   // delete ui;
}

void waitWidget::paintEvent(QPaintEvent *ev)
{
    static int b = 0;
    CircleDropData(myInCircle_r, myOutCircle_r,EQUALNUM,myInDrop, myOutDrop);
    QPen pen(Qt::black,2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    for(int a = 0;a< EQUALNUM;a++){
        pen.setColor(QColor::fromHsl(4,201,110+((a + b) % EQUALNUM) *8));
        myPainter[a]->begin(this);
        myPainter[a]->setRenderHint(QPainter::Antialiasing, true);
        myPainter[a]->setPen(pen);
        myPainter[a]->drawLine(myInDrop[a].x(),myInDrop[a].y(),myOutDrop[a].x(),myOutDrop[a].y());
        myPainter[a]->end();
    }
    b++;
    b %= EQUALNUM;
}

//将圆进行等分并取出对应的坐标，为后面划线打下基础
bool waitWidget::CircleDropData(int inCircle_r, int outCircle_r, int equalNum, QPoint *inDrop, QPoint *outDrop)
{
    if(inCircle_r > outCircle_r || equalNum == 0){
        return false;
    }
    double angleIncrement = 360.0 / equalNum;
    for(int a = 0;a < equalNum;a++){
        double angle = a * angleIncrement;
        double radian = angle * (PI / 180);
        inDrop[a].setX(inCirclePoint->x() + inCircle_r * cos(radian));
        inDrop[a].setY(inCirclePoint->y() + inCircle_r * sin(radian));
        outDrop[a].setX(outCirclePoint->x() + outCircle_r * cos(radian));
        outDrop[a].setY(outCirclePoint->y() + outCircle_r * sin(radian));
    }
    return true;
}

void waitWidget::loopTimerOut()
{
    emit this->repaint();
}
